package com.tianji.learning.listener;

import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.learning.service.ILearningLessonService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class OrderPaySuccessListener {

    //@Autowired
    private final ILearningLessonService learningLessonService;


    //更推荐使用构造函数做注入
    public OrderPaySuccessListener(ILearningLessonService learningLessonService){
        this.learningLessonService = learningLessonService;
    }

    // set方法注入
//    public void setLearningLessonService(ILearningLessonService learningLessonService){
//        this.learningLessonService = learningLessonService;
//    }

    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(name=MqConstants.Queue.ORDER_PAY_QUEUE),
            exchange = @Exchange(name=MqConstants.Exchange.ORDER_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.ORDER_PAY_KEY
    ))
    // TODO：查一下为啥这个地方可以直接使用DTO来接收？
    public void onOrderPaySuccess(OrderBasicDTO dto){
        // 遍历dto中的courseIds
        //   构造learning_lesson对象
        //      userId：参数中都有
        //      courseId：参数中都有
        //      expire_time：没有
        //        查询course服务，查询课程的有效期
        // 批量插入数据库（优化TODO：可以做一个幂等操作：先查询是否已经加入到课表了）
        learningLessonService.onOrderPaySuccess(dto);
    }
}
